<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <div id="app">
      <!-- 过滤器：文本格式化 -->
      <!-- 可以给双大括号，也可以给v-bind用 -->
      <!-- 过滤器还可以做串联 -->
      <!-- vue3删除了过滤器 -->
      <h2>过滤器</h2>
      <!-- <p>性别: {{ sex === 1? "男": "女" }}</p> -->
      <p>性别: {{ sex | formatSex | formatSex2 }}</p>
      <p>当前时间: {{ time | formatTime }}</p>
      <p :title="time | formatTime">123</p>
    </div>

    <script src="./lib/vue2.js"></script>
    <script>
      new Vue({
        el: "#app",
        data() {
          return {
            sex: 1,
            time: 1669258007318,
          };
        },
        filters: {
          formatSex(val) {
            return val === 1 ? "男" : "女";
          },
          formatSex2(val) {
            return val === "男" ? "♂" : "♀";
          },
          formatTime(val) {
            const date = new Date(val);
            const Y = date.getFullYear();
            const M = date.getMonth() + 1;
            const D = date.getDate();
            const hour = date.getHours();
            const min = date.getMinutes();
            const sec = date.getSeconds();
            return (
              Y +
              "-" +
              (M < 10 ? "0" + M : M) +
              "-" +
              (D < 10 ? "0" + D : D) +
              " " +
              (hour < 10 ? "0" + hour : hour) +
              ":" +
              (min < 10 ? "0" + min : min) +
              ":" +
              (sec < 10 ? "0" + sec : sec)
            );
          },
        },
      });
    </script>
  </body>
</html>
